/**
 入驻小区
 **/
(function(vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            buyGoldInfo: {
                poolId: '',
                actualPrice: '0',
                buyPrice: '0',
                couponName: '0',
                couponStock: '0',
                couponType: '',
                goldCount: '0',
                goldMoney: '0',
                payQrCode: '',
                shopId: vc.getCurrentCommunity().shopId,
                showRefresh: false
            }
        },
        _initMethod: function() {
            $that.buyGoldInfo.poolId = vc.getParam('poolId');
            $that._listProducts(DEFAULT_PAGE, DEFAULT_ROWS);
        },
        _initEvent: function() {
            vc.on('buyGold', 'listProduct', function(_param) {
                $that.buyGoldInfo.componentShow = 'buyGold';
                $that._listProducts(DEFAULT_PAGE, DEFAULT_ROWS);
            });
        },
        methods: {
            _listProducts: function(_page, _rows) {
                var param = {
                    params: {
                        page: 1,
                        poolId: $that.buyGoldInfo.poolId,
                        row: 1
                    }
                };
                //发送get请求
                vc.http.apiGet('couponPool.listCouponPool',
                    param,
                    function(json, res) {
                        let _buyGoldInfo = JSON.parse(json);
                        vc.copyObject(_buyGoldInfo.data[0], $that.buyGoldInfo);
                        $that._changeGoldCount();
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _closeBuyGold: function() {
                $('#buyGoldPayModel').modal('hide');
                vc.jumpToPage('/#/pages/goods/couponShopPoolManage');
            },
            _changeGoldCount: function() {
                let _goldMoney = $that.buyGoldInfo.buyPrice * $that.buyGoldInfo.goldCount;
                $that.buyGoldInfo.goldMoney = _goldMoney.toFixed(2);
            },
            _toBuyGold: function() {
                $that.buyGoldInfo.payQrCode = '';
                vc.http.apiPost(
                    '/paymentGold/toQrPayGold',
                    JSON.stringify($that.buyGoldInfo), {
                        emulateJSON: true
                    },
                    function(json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $that.buyGoldInfo.payQrCode = _json.codeUrl;
                            $that._viewQr($that.buyGoldInfo.payQrCode);
                            $('#buyGoldPayModel').modal('show');
                            vc.toast(_json.msg);
                            return;
                        }
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _viewQr: function(_url) {
                document.getElementById("qrcode").innerHTML = "";
                let qrcode = new QRCode(document.getElementById("qrcode"), {
                    text: "优惠券", //你想要填写的文本
                    width: 200, //生成的二维码的宽度
                    height: 200, //生成的二维码的高度
                    colorDark: "#000000", // 生成的二维码的深色部分
                    colorLight: "#ffffff", //生成二维码的浅色部分
                    correctLevel: QRCode.CorrectLevel.H
                });
                qrcode.makeCode(_url);
            },
            _finishScanQrCode: function() {
                 $that._closeBuyGold();
            }
        }
    });
})(window.vc);